import Vue from 'vue'
import axios from "@/utils/axios.min.js";
import qs from "qs";

let isFirstQrReq=true;

let vm = Vue.prototype;

// 创建axios实例
const httpService = axios.create({
    //API地址
    baseURL: 'http://user.miyun.info',
    // 请求超时时间
    timeout: 30000,
    withCredentials: true
});
const writeService = axios.create({
    //API地址
    baseURL: 'http://open.devep.cn',
    // 请求超时时间
    timeout: 30000,
    withCredentials: true
});


writeService.interceptors.request.use(
    config => {
        console.log("token:",window.localStorage.getItem("u_Token"));
        if(window.localStorage.getItem("u_Token")!==null){
          config.headers['token'] = window.localStorage.getItem("u_Token");
        }


        return config;
    },
    error => {
        // 请求错误处理
        Promise.reject(error);
    }
);

// request拦截器

// Cannot create property 'headers' on boolean 'false'
// https://blog.csdn.net/qq_31906983/article/details/88846570
httpService.interceptors.request.use(
    config => {
        console.log("token:",window.localStorage.getItem("u_Token"));
        if(window.localStorage.getItem("u_Token")!==null){
          config.headers['token'] = window.localStorage.getItem("u_Token");
        }


        return config;
    },
    error => {
        // 请求错误处理
        Promise.reject(error);
    }
);

// response拦截器
httpService.interceptors.response.use(
    response => {
        // 统一处理状态
        console.log("响应成功：",response.config.url);
          console.log("isFirstQrReq:",isFirstQrReq);
        if(response.config.url=='/passport/queryOfficialAccount'){
          if(isFirstQrReq){
            if(response.data.code==200){
              vm.$bus.$emit("toastEvent",response.data.msg);
              isFirstQrReq=false;
            }

            // else if(response.data.code==10000){
            //   vm.$bus.$emit("toastEvent",response.data.msg);
            // }else if(response.data.code==10444){
            //   vm.$bus.$emit("toastEvent",response.data.msg);
            //   vm.$bus.$emit("op_manage_Event",true,'PhoneNum');
            // }else if(response.data.code==10445){
            //   vm.$bus.$emit("toastEvent",response.data.msg);
            // }
          }else{
            console.log("=============出错=============");
          }
        }else if(response.config.url=='/log/coin'||response.config.url=='/log/order'||response.config.url=='/log/money'||response.config.url=='/log/score'||response.config.url=='/log/recharge'){
          if(response.data.code==200){
              //vm.$bus.$emit("toastEvent",response.data.msg);
            }else if(response.data.code==10000){
              vm.$bus.$emit("toastEvent",response.data.msg);
            }else if(response.data.code==10444){
              vm.$bus.$emit("toastEvent",response.data.msg);
              vm.$bus.$emit("op_manage_Event",true,'PhoneNum');
            }else if(response.data.code==10445){
              vm.$bus.$emit("toastEvent",response.data.msg);
            }

        }else if(response.config.url=='/order/queryPayResult'){
          console.log("isFirstQrReq:",isFirstQrReq);
          if(isFirstQrReq){
            // if(response.data.code==200){
            //   vm.$bus.$emit("toastEvent",response.data.msg);
            //   isFirstQrReq=false;
            // }else if(response.data.code==10000){
            //   isFirstQrReq=false;
            //   vm.$bus.$emit("toastEvent",response.data.msg);
            // }else if(response.data.code==10444){
            //   vm.$bus.$emit("toastEvent",response.data.msg);
            //   vm.$bus.$emit("op_manage_Event",true,'PhoneNum');
            // }else if(response.data.code==10445){
            //   vm.$bus.$emit("toastEvent",response.data.msg);
            // }

            if(response.data.code==200){
                vm.$bus.$emit("toastEvent",response.data.msg);
                isFirstQrReq=false;
              }

              // else if(response.data.code==10000){
              //   isFirstQrReq=false;
              //   vm.$bus.$emit("toastEvent",response.data.msg);
              // }else if(response.data.code==10444){
              //   vm.$bus.$emit("toastEvent",response.data.msg);
              //   vm.$bus.$emit("op_manage_Event",true,'PhoneNum');
              // }else if(response.data.code==10445){
              //   vm.$bus.$emit("toastEvent",response.data.msg);
              // }

          }else{
            //console.log("=============出错=============");
          }
        }else if(response.config.url=='/user/bindMobile'){
          vm.$bus.$emit("toastEvent",response.data.msg);
        }else if(response.config.url=='/user/bindWechat'){

        }else{
          if(response.data.code==200){
            //vm.$bus.$emit("toastEvent",response.data.msg);
          }else if(response.data.code==10000){
            vm.$bus.$emit("toastEvent",response.data.msg);
          }else if(response.data.code==10444){
            vm.$bus.$emit("toastEvent",response.data.msg);
            vm.$bus.$emit("op_manage_Event",true,'PhoneNum');
          }else if(response.data.code==10445){
            vm.$bus.$emit("toastEvent",response.data.msg);
          }
        }


        return response; // response一定要返回
    },
    // 处理处理
    error => {
        console.log("响应出错：",error.response);
        // if(error.response.data.msg === "请先登录"){
        //   vm.$bus.$emit("op_AcpopEvent",true);
        // }
        // vm.$bus.$emit("toastEvent",error.response.data.msg);

        return false;
    }
);


/*
 *  get请求
 *  url:请求地址
 *  params:参数
 * */
export function get(url, params = {},headObj={}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'get',
            params: params,
            headers:headObj
        }).then(response => {
           resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

/*
 *  post请求
 *  url:请求地址
 *  params:参数
 * */
export function post(url, params = {},headObj={}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'post',
            data: qs.stringify(params),
            headers:headObj
        }).then(response => {
             resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

/*
 *  文件上传
 *  url:请求地址
 *  params:参数
 * */
export function fileUpload(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'post',
            data: params,
            headers: {'Content-Type': 'multipart/form-data'}
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

export function writePost(url, params = {},headObj={}) {
    return new Promise((resolve, reject) => {
        writeService({
            url: url,
            method: 'post',
            data: qs.stringify(params),
            headers:headObj
        }).then(response => {
             resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

export default {
    get,
    post,
    fileUpload,
    writePost
}
